**************************************************************
* ELECTRICITY SPILOVERS PROJECT
*  SUMMARY STATISTICS AND BALANCE TESTS
**************************************************************
set more off
 
***************
*TABLE 1 - BALANCE (HH Characteristics)
frame copy data_main balance
frame change balance

drop if date<=20223 
drop if flag_outlier==1
drop if flag_euse==1
drop if elec_use_summer==.

keep HHID ws control yearbuilt sqftmain bedrooms bathrooms totalvalue pool ///
     elec_use_summer elec_use_winter elec_use_annual w_use_winter ///
	 w_use_summer w_use_annual
gduplicates drop
 
 
**** 
*Table: Balance Tests
orth_out elec_use_annual elec_use_summer w_use_annual  yearbuilt bedrooms bathrooms sqftmain totalvalue pool ///
	using $tables\balance_hhchar_$outputdate.tex, ///
	by(ws) se compare test vcount count stars latex full replace bdec(2) 
foreach v in elec_use_annual elec_use_summer yearbuilt bedrooms bathrooms sqftmain totalvalue pool w_use_annual {
ttest `v', by(ws)
}
frame change data_main
frame drop balance 

**************************************************************
*TABLE 1 - BALANCE (PRE-TREATMENT MONTHLY BILLING ELECTRICITY)
**************************************************************
frame create balance
frame change balance
use $clean\elecbilling_$outputdate.dta, replace

drop if flag_outlier==1
drop if elec_use_summer==.
*t-tests
forvalues i=1(1)12 {
display `i'
ttest elec_use if start_month==`i', by(WS)
}
*
*****************************************	
*FIGURE 2 - BALANCE (PRE-TREATMENT MONTHLY ELECTRICITY USE)
**********************************************
gen ym=.
gen con=.
gen con_ub=.
gen con_lb=.
gen treat=.
gen treat_ub=.
gen treat_lb=.
gen dif=.
gen dif_lb=.
gen dif_ub=.

forvalues i = 650(1)661 {
local j = `i'
replace ym=`i' in `j'

sum elec_use if WS==0 & ym_start==`i'
	scalar euse`i' = r(mean)	
	scalar euse`i'_sd=r(sd)
	replace con=scalar(euse`i') in `j'
	replace con_ub=scalar(euse`i')+1.96*scalar(euse`i'_sd) in `j'
	replace con_lb=scalar(euse`i')-1.96*scalar(euse`i'_sd) in `j'
	
sum elec_use if WS==1 & ym_start==`i'
	scalar euse`i' = r(mean)	
	scalar euse`i'_sd=r(sd)
	replace treat=scalar(euse`i') in `j'
	replace treat_ub=scalar(euse`i')+1.96*scalar(euse`i'_sd) in `j'
	replace treat_lb=scalar(euse`i')-1.96*scalar(euse`i'_sd) in `j'	
	
ttest elec_use if ym_start==`i', by(WS)
	scalar dif_se`i'=r(se)
 	replace dif=con-treat in `j'
	replace dif_ub=dif+1.96*scalar(dif_se`i') in `j'
	replace dif_lb=dif-1.96*scalar(dif_se`i') in `j'

}
*
*Graphing
twoway scatter treat ym, m(diamond) mc(cranberry) msize(medlarge)  || ///
	   scatter con ym, m(circ) mc(dkorange) msize(medlarge) ///
	   graphregion(color(white)) bgcolor(white) ///
	   title("Baseline Electricity Use By Month") ///
	   xtitle("") ///  
	   ytit("Avg. Electricity Use (kWh/month)") ///
	   legend(order(2 "Control" 1 "Treatment" ) ///
	      region(lcolor(white)) cols(1) ring(0) position(11)) ///
	   xlabel(650 "3/2014" 652 "5/2014" 654 "7/2014" 656 "9/2014" 658 "11/2014" 660 "1/2015") yline(0,lc(black))  ///	   
	   ylabel(450(150)1050, nogrid)	   
graph save $figs\balance_eusemth1_$outputdate.gph, replace
	   
twoway rcap dif_ub dif_lb ym, lstyle(ci) lc(erose) || ///
	   scatter dif ym, m(triangle) mc(dknavy) msize(medlarge) /// 
	   graphregion(color(white)) bgcolor(white) ///
	   title("Difference in Baseline Electricity Use By Month") ///
	   xtitle("") ///  
	   ytit("Avg. Electricity Use (kWh/month)") ///
	   legend(order(2 "Difference" ) ///
	      region(lcolor(white)) cols(1) ring(0) position(11)) ///
	  xlabel(650 "3/2014" 652 "5/2014" 654 "7/2014" 656 "9/2014" 658 "11/2014" 660 "1/2015") yline(0,lc(black)) ///	   
	   ylabel(-20(10)30, nogrid)	   
graph save $figs\balance_eusemth2_$outputdate.gph, replace	   
graph combine "$figs\balance_eusemth2_$outputdate.gph" "$figs\balance_eusemth1_$outputdate.gph", colfirst xcommon cols(1) xsize(7.0)	   
graph export $figs\balance_eusemthcombine_$outputdate.png, width(4000) replace

frame change data_main
frame drop balance 